package com.ggh.jinjilive.ui.mine.personal.dressup.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemDressUpBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mine.personal.DressUpDataMobile;

import java.util.List;

public class DressUpAdapter extends RecyclerView.Adapter<DressUpAdapter.Holder> {
    private List<DressUpDataMobile> dressUpDataMobileList;

    public void setAdd(List<DressUpDataMobile> dressUpDataMobileList){
        this.dressUpDataMobileList = dressUpDataMobileList;
        notifyDataSetChanged();
    }

    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener){
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemDressUpBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_dress_up, parent, false);
        return new Holder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        DressUpDataMobile dressUpDataMobile = dressUpDataMobileList.get(position);
        if(null != dressUpDataMobile){
            //图标
            Glide.with(holder.binding.imgPicture.getContext()).load(dressUpDataMobile.getImage()).into(holder.binding.imgPicture);
            //钻石
            holder.binding.tvDiamonds.setText(dressUpDataMobile.getDiamonds());
            //购买
            if(dressUpDataMobile.getIs_buy() == 1){
                if(dressUpDataMobile.getIs_default() == 0){
                    holder.binding.tvDiamonds.setVisibility(View.INVISIBLE);
                    holder.binding.tvConfirm.setVisibility(View.VISIBLE);
                    holder.binding.tvConfirm.setText("使用");
                    holder.binding.llBg.setBackgroundResource(0);
                }else {
                    holder.binding.tvDiamonds.setVisibility(View.INVISIBLE);
                    holder.binding.tvConfirm.setVisibility(View.VISIBLE);
                    holder.binding.tvConfirm.setText("取消");
                    holder.binding.llBg.setBackgroundResource(R.drawable.bg_5_f_66);
                }
            }else {
                holder.binding.tvDiamonds.setVisibility(View.VISIBLE);
                holder.binding.tvConfirm.setVisibility(View.VISIBLE);
                holder.binding.tvConfirm.setText("确认");
                holder.binding.llBg.setBackgroundResource(0);
            }
            //确认
            holder.binding.tvConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,dressUpDataMobile);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == dressUpDataMobileList ? 0 : dressUpDataMobileList.size();
    }

    public class Holder extends RecyclerView.ViewHolder {
        ItemDressUpBinding binding;

        public Holder(@NonNull ItemDressUpBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}